<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>打开一个网络连接或者一个Unix套接字连接</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.fsockopen" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">fsockopen</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">fsockopen</span> &mdash; <span class="dc-title">打开一个网络连接或者一个Unix套接字连接</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.fsockopen-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>fsockopen</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$hostname</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$port</code><span class="initializer"> = -1</span></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter reference">&$errno</code></span>
   [, <span class="methodparam"><span class="type">string</span> <code class="parameter reference">&$errstr</code></span>
   [, <span class="methodparam"><span class="type">float</span> <code class="parameter">$timeout</code><span class="initializer"> = ini_get(&quot;default_socket_timeout&quot;)</span></span>
  ]]]] ) : <span class="type">resource</span></div>

  <p class="para rdfs-comment">
   初始化一个套接字连接到指定主机（<code class="parameter">hostname</code>）。
  </p>
  <p class="para">
   PHP支持以下的套接字传输器类型列表
   <a href="transports.html" class="xref">所支持的套接字传输器（Socket Transports）列表</a>。也可以通过<span class="function"><a href="stream_get_transports.html" class="function">stream_get_transports()</a></span>来获取套接字传输器支持类型。
  </p>
  <p class="para">
   默认情况下将以阻塞模式开启套接字连接。当然你可以通过<span class="function"><a href="stream_set_blocking.html" class="function">stream_set_blocking()</a></span>将它转换到非阻塞模式。
  </p>
  <p class="para"><span class="function"><a href="stream_socket_client.html" class="function">stream_socket_client()</a></span>与之非常相似，而且提供了更加丰富的参数设置，包括非阻塞模式和提供上下文的的设置。
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.fsockopen-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">hostname</code></dt>

     <dd>

      <p class="para">
       如果安装了<a href="openssl.installation.html" class="link">OpenSSL</a>，那么你也许应该在你的主机名地址前面添加访问协议<em>ssl://</em>或者是<em>tls://</em>，从而可以使用基于TCP/IP协议的SSL或者TLS的客户端连接到远程主机。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">port</code></dt>

     <dd>

      <p class="para">
       端口号。如果对该参数传一个-1，则表示不使用端口，例如<em>unix://</em>。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">errno</code></dt>

     <dd>

      <p class="para">
       如果传入了该参数，holds the system level error number that occurred in the
       system-level <em>connect()</em> call。
      </p>
      <p class="para">
       如果<code class="parameter">errno</code>的返回值为<em>0</em>，而且这个函数的返回值为<strong><code>FALSE</code></strong>，那么这表明该错误发生在套接字连接（<em>connect()</em>）调用之前，导致连接失败的原因最大的可能是初始化套接字的时候发生了错误。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">errstr</code></dt>

     <dd>

      <p class="para">
       错误信息将以字符串的信息返回。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">timeout</code></dt>

     <dd>

      <p class="para">
       设置连接的时限，单位为秒。
      </p>
      <blockquote class="note"><p><strong class="note">Note</strong>: 
       <p class="para">
        注意：如果你要对建立在套接字基础上的读写操作设置操作时间设置连接时限，请使用<span class="function"><a href="stream_set_timeout.html" class="function">stream_set_timeout()</a></span>，<span class="function"><strong>fsockopen()</strong></span>的连接时限（<code class="parameter">timeout</code>）的参数仅仅在套接字连接的时候生效。
       </p>
      </p></blockquote>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.fsockopen-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   <span class="function"><strong>fsockopen()</strong></span>将返回一个文件句柄，之后可以被其他文件类函数调用（例如：<span class="function"><a href="fgets.html" class="function">fgets()</a></span>，<span class="function"><a href="fgetss.html" class="function">fgetss()</a></span>，<span class="function"><a href="fwrite.html" class="function">fwrite()</a></span>，<span class="function"><a href="fclose.html" class="function">fclose()</a></span>还有<span class="function"><a href="feof.html" class="function">feof()</a></span>）。如果调用失败，将返回<strong><code>FALSE</code></strong>。
  </p>
 </div>

 <div class="refsect1 errors" id="refsect1-function.fsockopen-errors">
  <h3 class="title">错误／异常</h3>
  <p class="para">
   如果主机（<code class="parameter">hostname</code>）不可访问，将会抛出一个警告级别（<strong><code>E_WARNING</code></strong>）的错误提示。
  </p>
 </div>

 <div class="refsect1 changelog" id="refsect1-function.fsockopen-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>4.3.0</td>
       <td>
        在win32系统上增加了对时限设置（<code class="parameter">timeout</code>）参数的支持。
       </td>
      </tr>

      <tr>
       <td>4.3.0</td>
       <td>
        在TCP/IP协议的基础上增加了SSL和TLS。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-function.fsockopen-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-5482">
    <p><strong>Example #1 <span class="function"><strong>fsockopen()</strong></span>的例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$fp&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fsockopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"www.example.com"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">80</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$errno</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$errstr</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">30</span><span style="color: #007700">);<br />if&nbsp;(!</span><span style="color: #0000BB">$fp</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$errstr</span><span style="color: #DD0000">&nbsp;(</span><span style="color: #0000BB">$errno</span><span style="color: #DD0000">)&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$out&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"GET&nbsp;/&nbsp;HTTP/1.1\r\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$out&nbsp;</span><span style="color: #007700">.=&nbsp;</span><span style="color: #DD0000">"Host:&nbsp;www.example.com\r\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$out&nbsp;</span><span style="color: #007700">.=&nbsp;</span><span style="color: #DD0000">"Connection:&nbsp;Close\r\n\r\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$out</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;(!</span><span style="color: #0000BB">feof</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">fgets</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">128</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-5483">
    <p><strong>Example #2 使用UDP连接</strong></p>
    <div class="example-contents"><p>
     下面这个例子展示了怎么样在自己的机器上通过UDP套接字连接（端口号13）来检索日期和时间。
    </p></div>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$fp&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fsockopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"udp://127.0.0.1"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">13</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$errno</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$errstr</span><span style="color: #007700">);<br />if&nbsp;(!</span><span style="color: #0000BB">$fp</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"ERROR:&nbsp;</span><span style="color: #0000BB">$errno</span><span style="color: #DD0000">&nbsp;-&nbsp;</span><span style="color: #0000BB">$errstr</span><span style="color: #DD0000">&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">fread</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">26</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
 </div>

 <div class="refsect1 notes" id="refsect1-function.fsockopen-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    因为环境的不同，某些情况下在Unix套接字连接或者自定义的连接设置连接时限（<code class="parameter">timeout</code>）可能不会生效。
   </p>
  </p></blockquote>
  <div class="warning"><strong class="warning">Warning</strong>
   <p class="para">
    UDP套接字有些时候在即使远程主机未知的情况，也能打开，并且不发生任何错误。只有当你通过该套接字进行读写的时候才会发现错误。之所以会这样，是因为UDP是一个“非连接状态”的协议，那么这就意味着当前操作系统直到它（套接字）真正需要发送和接受数据的时候才会去尝试为其去建立连接。
   </p>
  </div>
  <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">当指定数值型的 IPv6
地址（例如 <em>fe80::1</em>）时必须用方括号将 IP 围起来——例如，
<em>tcp://[fe80::1]:80</em>。</span></p></blockquote>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.fsockopen-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="pfsockopen.html" class="function" rel="rdfs-seeAlso">pfsockopen()</a> - 打开一个持久的网络连接或者Unix套接字连接。</span></li>
    <li class="member"><span class="function"><a href="stream_socket_client.html" class="function" rel="rdfs-seeAlso">stream_socket_client()</a> - Open Internet or Unix domain socket connection</span></li>
    <li class="member"><span class="function"><a href="stream_set_blocking.html" class="function" rel="rdfs-seeAlso">stream_set_blocking()</a> - 为资源流设置阻塞或者阻塞模式</span></li>
    <li class="member"><span class="function"><a href="stream_set_timeout.html" class="function" rel="rdfs-seeAlso">stream_set_timeout()</a> - Set timeout period on a stream</span></li>
    <li class="member"><span class="function"><a href="fgets.html" class="function" rel="rdfs-seeAlso">fgets()</a> - 从文件指针中读取一行</span></li>
    <li class="member"><span class="function"><a href="fgetss.html" class="function" rel="rdfs-seeAlso">fgetss()</a> - 从文件指针中读取一行并过滤掉 HTML 标记</span></li>
    <li class="member"><span class="function"><a href="fwrite.html" class="function" rel="rdfs-seeAlso">fwrite()</a> - 写入文件（可安全用于二进制文件）</span></li>
    <li class="member"><span class="function"><a href="fclose.html" class="function" rel="rdfs-seeAlso">fclose()</a> - 关闭一个已打开的文件指针</span></li>
    <li class="member"><span class="function"><a href="feof.html" class="function" rel="rdfs-seeAlso">feof()</a> - 测试文件指针是否到了文件结束的位置</span></li>
    <li class="member"><span class="function"><a href="socket_connect.html" class="function" rel="rdfs-seeAlso">socket_connect()</a> - 开启一个套接字连接</span></li>
    <li class="member">The <a href="ref.curl.html" class="link">Curl extension</a></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>